/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-03
 * Time: 16:12
 */
define(function (require, exports, module) {
	'use strict';
    
    require('../list/rescenterListCtrl');
    
    var rk = require('rk');

    $.widget('rk.rescenterSearchCtrl',$.rk.rescenterListCtrl, {
        options: {
            selector:null,
            gId: null,
            key: null,
            size: 20,
            page: 1,
            url:null,
            postProcessor:null,
            preProcessor:null
        },
        pagingBar:null,
        _create: function () {
            var me = this;
            var elem = me.element;
            //me._removeListContainer();
            //me._super();


            //me.search();
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._render();
            me._bindHandler();

        },

        _removeListContainer:function(){
            var me = this;
            if(me._getListContainer().size()) me._getListContainer().off().empty().removeClass().addClass('oa-list oa-rescenter-list clear');

        },


        _getSearchContainer: function () {
            var me = this;
            return me.getPanelContent().find('div.announce-search-box');
        },

        _render: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            //var search_tpl = require('oa/tmpl/rescenter/tpl_rescenter_search.tpl');
            me._getListPanelContainer().html(rk.templateText(require('oa/tmpl/rescenter/tpl_rescenter_search.tpl')));
        },

        _bindHandler: function (){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if( !me._getTabHeaderContainer().data('init')){
                me._getTabHeaderContainer().on('click','a',function(event){
                    event.preventDefault();
                    opt.page =1;
                    me._trigger('postProcessor',event,null);
                    return false;
                });
                me._getTabHeaderContainer().data('init',1);
            }


        },

        search:function (){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            opt.gId = me._getGroup().groupId;
            opt.url = me._getGroup().search_action;
            me._loadData();

        },

        _loadData:function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var $btn = $(opt.selector);
            var key = $.isFunction(opt.key)? opt.key.call($btn): (window.console && console.error('key is not a function'));
            me._trigger('preProcessor',null,null);
            //me._getListContainer().a('搜索中...');
            me._getListContainer().beLoading();
            me._getListContainer().beScroll();
            me._setTotal(0);
            rk.get(opt.url, {
                gId: opt.gId,
                key: key,
                page:(opt.page - 1),
                size:opt.size
            }, function (data) {
                if (data.status === 0) {
                    data = data.data;
                    if ($.isEmptyObject(data.twitterFiles)) {
                        me._blank(rk.i18n('TWITTERFILE_SEARCH_RESULTS_EMPTY'));
                        return;
                    }
                    $.each(data.twitterFiles, function (idx, _tf) {
                        _tf.owner = data.users[_tf.userId].name;
                        me._assembleTplData(_tf);
                    });
                    data.session = SESSION;
                    var list_HTML = rk.templateText(require('oa/tmpl/rescenter/tpl_rescenter_list.tpl'), data);
                    me._getListContainer().html(list_HTML);
                    me._setTotal(data.total);
                        me.pagingBar = me.getPanel().find('.oa-listview-pagination');
                        me.pagingBar.rkPaging({
                            pagenum: opt.page,
                            pagesize: opt.size,
                            rowscount: data.total,
                            onChange: function(pagenum){
                                opt.page = pagenum;
                                me._loadData();
                            }
                        });
                }
            });

        },

        _setTotal:function (num){
            this._getTabHeaderContainer().find('span.number').text(num);

        },


    });

    return $;

});